package cn.edu.xust.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class BCDUtils {

    /**
     * 两个为一组 倒序
     *
     * @param str 12345678
     * @return    78563412
     */
    public static String reverseSplit(String str) {
        if (str.length() % 2 != 0) {
            return "err";
        }
        List<String> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i += 2) {
            list.add(str.substring(i, Math.min(i + 2, str.length())));
        }
        for (int i = list.size() - 1; i > -1; i--) {
            sb.append(list.get(i));
        }
        return sb.toString();
    }


    /**
     *  反转且拼凑两个
     *  用于电表的八位数字
     * @param str1 1234
     * @param str2 5678
     * @return  78563412
     */
    public static String reverseAndCombine(String str1, String str2) {
        List<String> pairs = new ArrayList<>();

        for (int i = 0; i < str1.length(); i += 2) {
            if (i + 1 < str1.length()) {
                pairs.add(str1.substring(i, i + 2));
            } else {
                pairs.add(str1.substring(i));
            }
        }

        for (int i = 0; i < str2.length(); i += 2) {
            if (i + 1 < str2.length()) {
                pairs.add(str2.substring(i, i + 2));
            } else {
                pairs.add(str2.substring(i));
            }
        }

        StringBuilder reversed = new StringBuilder();
        for (int i = pairs.size() - 1; i >= 0; i--) {
            reversed.append(pairs.get(i));
        }

        return reversed.toString();
    }

    public static void main(String[] args) {
        System.out.println( reverseAndCombine("1234","5678"));
    }

    /**
     * 反转mac  且俩俩分隔
     * @param input  123456789088
     * @return 88 90 78 56 34 12
     */
    public static String processString(String input) {
        StringBuilder sb = new StringBuilder();
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < input.length(); i += 2) {
            String pair = input.substring(i, i + 2);
            list.add(pair);
        }
        Collections.reverse(list);
        String res = String.join(" ", list);
        return res.trim().toUpperCase();
    }


    /**
     * 俩俩分隔
     *
     * @param input 445566
     * @return 44 55 66
     */
    public static String separateString(String input) {
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < input.length(); i += 2) {
            stringBuilder.append(input.charAt(i));
            stringBuilder.append(input.charAt(i + 1));
            stringBuilder.append(" ");
        }

        return stringBuilder.toString().trim();
    }


    /**
     * +3且分隔
     *
     * @param input 23 25 27
     * @return      56 58 5A
     */
    public static String convertToHexString(String input) {
        StringBuilder result = new StringBuilder();
        for (char c : input.toCharArray()) {
            if (c != ' ') {
                String hexValue = Integer.toHexString(Character.digit(c, 10) + 3).toUpperCase();
                result.append(hexValue);
            }
        }
        return separateString(result.toString());
    }
}
